//
// Created by gogoqiu on 2022/2/11.
//

#ifndef RAPIDSVN_CMAKE_PREFERENCECORE_H
#define RAPIDSVN_CMAKE_PREFERENCECORE_H


#include <model/PreferenceCore.h>

#define KEY_MY_KEY1  "key_my_key1"
#define KEY_MY_KEY2  "key_my_key2"
#define KEY_QUERY_GIT_URL  "key_query_git_url"
#define KEY_GIT_IP  "key_git_ip"
#define KEY_IS_IN_LAN  "key_is_in_lan"
//是否自动启动网络状态捕捉线程
#define KEY_AUTORUN_WIFI_MONITOR "key_autorun_wifi_monitor"


class MyPreferenceCore: public PreferenceCore{
public:
    /*
     * buildViewList
     *
     */
    explicit MyPreferenceCore( MyTagApp * myTagApp1 );

protected:
    /*
     * put items to sqlite as default/value
     * 如何注入数据库
     * build key-values
     */
    void buildList() override {
        //wxString key, wxString title, PreferenceViewType type
        //比对作用
        //example
        list.push_back(new ViewList(KEY_MY_KEY1, "title1", PT_INPUT ));
        //example
        list.push_back(new ViewList(KEY_MY_KEY2, "title2", PT_INPUT ));
        list.push_back(new ViewList(KEY_QUERY_GIT_URL, _T( "获取GIT地址URL" ), PT_INPUT ));
        list.push_back(new ViewList(KEY_GIT_IP, _T( "GIT地址" ), PT_INPUT ));
        list.push_back(new ViewList(KEY_IS_IN_LAN, _T( "是否内网" ), PT_SWITCH ));
        //
    }

    /*
     * init the app data to the preference.db
     * setup the preference.db
     */
    void OnWithoutPreferenceTable(  SqlitePreferenceStorage* sqlitePreferenceStorage ) override{
        /*
         * SqlitePreferenceStorage::Load()
         *
         *
         * const wxString &key, const wxString &value,
                   const wxString &defaultValue,
                   PreferenceViewType type, const wxString& title
         */
        //init the table
        vector<PreferenceItem*> items;
        items.push_back( new PreferenceItem( KEY_MY_KEY1, "value1",
                                             "value1" ));
        items.push_back( new PreferenceItem( KEY_MY_KEY2, "value2",
                                             "value2" ));
        if ( storage->Save( items )==-1 ){

        };
    }

    /**
     * 避免碰撞
     */
    void OnPreferencePreLoad(SqlitePreferenceStorage* sqlitePreferenceStorage ) override{
        //every load run
        //OnPreLoad
        vector<PreferenceItem*> items;
        items.push_back( new PreferenceItem( KEY_QUERY_GIT_URL, "http://139.196.234.168/usr/bin/ip.php?type=check_lan",
                                             "http://139.196.234.168/usr/bin/ip.php?type=check_lan" ));
        items.push_back( new PreferenceItem( KEY_GIT_IP, "192.168.5.200",
                                             "192.168.5.2001" ));
        items.push_back( new PreferenceItem( KEY_IS_IN_LAN, "true",
                                             "true" ));
        //delete all and save all
        //storage->Save( items );
        //
    }

public:
    //setup the query function
    wxString get_query_git_url(){
        return QueryValue( KEY_QUERY_GIT_URL );
    }

    //setup the query function
    wxString get_git_ip(){
        return QueryValue( KEY_GIT_IP );
    }

    wxString get_is_in_lan(){
        return QueryValue( KEY_IS_IN_LAN );
    }

    //void adapter();
    wxString get_is_autorun_wifi_monitor(  ){
        return QueryValue( KEY_AUTORUN_WIFI_MONITOR );
    }
};

#endif //RAPIDSVN_CMAKE_PREFERENCECORE_H
